using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace WXB.RenderNode
{
	public enum HorzFormatting
	{
		LeftAligned,
		RightAligned,
		HorzCentred,
	};

	public enum VertFormatting
	{
		TopAligned,
		BottomAligned,
		VertCentred,
	};

	public abstract class NodeBase
	{
		public NodeBase()
		{
			d_bGlint = false;
			d_alpha = 1.0f;
			d_bNewLine = false;
			d_elapsed = 0.0f;
            d_beginLine = 0xffffffff;
			d_endLine = 0xffffffff;
		}

		protected static float AlignedFormatting(HorzFormatting formatting, float maxWidth, float curWidth)
		{
			float value = 0.0f;
			switch (formatting)
			{
				case HorzFormatting.RightAligned:
					value = (maxWidth - curWidth);
					break;

				case HorzFormatting.HorzCentred:
					value = (maxWidth - curWidth) / 2;
					break;
			}

			return value;
		}

		public abstract string getType();
		public abstract string getShowText();
        public abstract void init(object obj);
		public abstract string getAppend();
		public abstract float getHeight();
		public abstract float getWidth();
		public abstract NodeBase clone();

		public virtual bool update(float fTime)
		{
            return false;
		}

		public void setNewLine(bool line)
		{
			d_bNewLine = line;
		}

		public bool isNewLine() 
		{
			return d_bNewLine;
		}

		public abstract void render(
			RenderCache cache,
			ref float x,
			ref uint  yline,
			float zOffset,
			List<Vector2> vLineSize,
			Rect targetRect,
			Rect cliptRect,
			HorzFormatting xFormatting,
			float offsetX,
			float offsetY,
			float fAlpha);

        public void fill(ref float x, List<Vector2> offsets, float maxWidth)
		{
            UpdateWidthList();
			float height = getHeight();

            Vector2 backPoint = offsets[offsets.Count - 1];
			if (height > backPoint.y)
				backPoint.y = height;
			offsets[offsets.Count - 1] = backPoint;

			for (int i = 0; i < d_widthList.Count; ++i)
			{
                x += d_widthList[i];
				if (x > maxWidth)
				{
                    Vector2 nextLine = offsets[offsets.Count - 1];
                    nextLine.x = x - d_widthList[i];
					offsets[offsets.Count - 1] = nextLine;
                    offsets.Add(new Vector2(0.0f, height));

                    x = d_widthList[i];
				}
			}

			Vector2 lastPoint = offsets[offsets.Count - 1];
			lastPoint.x = x;
			offsets[offsets.Count - 1] = lastPoint;

			if (d_bNewLine)
			{
                offsets.Add(new Vector2(0, 0.0f));
				x = 0;
			}
		}

        protected List<float> d_widthList = new List<float>();
        protected virtual void UpdateWidthList()
		{
            d_widthList.Clear();
            d_widthList.Add(getWidth());
		}

        public List<UIWidget> UIWidgetList = new List<UIWidget>();
		public virtual void onMouseEnter()
		{

		}

		public virtual void onMouseLeave()
		{

		}

        public int MouseCount = 0;

		protected bool       d_bGlint;
		protected float      d_alpha;
		protected float      d_elapsed;
		protected bool       d_bNewLine;
		protected uint       d_beginLine;
		protected uint       d_endLine;
	};
}


